<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/13
 * Time: 9:31
 * 自定义的辅助函数
 */

// 以GET方式获取数据，替代file_get_contents
function get_data($url, $timeout = 5)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 跳过证书检查
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);//不检查证书

    $file_contents = curl_exec($ch);
    curl_close($ch);
    return $file_contents;
}

//获取数据库中配置
function web_config($field)
{
    if(!$field){
        return null;
    }
    return \App\Models\WebConfig::where('name',$field)->value('value')?:null;
}

//生成订单
function order_sn()
{
    return date('ymd').substr(time(),-5).substr(microtime(),2,5);
}
//通过订单id,获取订单商品名称
function getOrderGoodsName($order_id)
{
    if(!$order_id){
        return '';
    }
    $orderGoodsName=\App\Models\OrderGoods::where('order_id',$order_id)->pluck('goods_title');
    $str='';
    foreach ($orderGoodsName as $v){
        $str.=$v.',';
    }
    return $str;
}
// 处理带Emoji的数据，type=0表示写入数据库前的emoji转为HTML，为1时表示HTML转为emoji码,O:入库，1：出库
function deal_emoji($msg, $type = 1) {
    if ($type == 0) {
        $msg = urlencode ( $msg );
        $msg = json_encode ( $msg );
    } else {
        $msg = preg_replace_callback ( "#\\\u([0-9a-f]+)#i", function ($r) {
            return iconv ( 'UCS-2', 'UTF-8', pack ( 'H4', '\\1' ) );
        }, $msg );
        $msg = urldecode ( $msg );
        $msg = str_replace ( '"', "", $msg );
    }
    return $msg;
}


//将数组写入调试日志文件
function log_arr($arr){
    $str=var_export($arr,true);
    $url='/var/www/rentmaterial/storage/logs/debug.log';
    file_put_contents($url,$str);//日志，便于调试
}